#include "wxsflexgridsizer.h"

namespace {
  wxArrayInt GetArray( const wxString& String, bool* Valid = 0 ) {
    wxStringTokenizer Tokens( String, _T( "," ) );
    wxArrayInt Array;
    if( Valid ) {
      *Valid = true;
    }
    while( Tokens.HasMoreTokens() ) {
      long Value;
      wxString Token = Tokens.GetNextToken();
      Token.Trim( true );
      Token.Trim( false );
      if( !Token.ToLong( &Value ) && Valid ) {
        *Valid = false;
      }
      Array.Add( ( int )Value );
    }
    return Array;
  }

  bool FixupList( wxString& List ) {
    bool Ret;
    wxArrayInt Array = GetArray( List, &Ret );
    List.Clear();
    for( size_t i = 0; i < Array.Count(); i++ ) {
      List.Append( wxString::Format( _T( "%d" ), Array[i] ) );
      if( i < Array.Count() - 1 ) {
        List.Append( _T( ',' ) );
      }
    }
    return Ret;
  }
  wxsRegisterItem<wxsFlexGridSizer> Reg( _T( "FlexGridSizer" ), _T( "FlexGridSizer" ), wxsTSizer, _T( "布局" ), 80 );
}

wxsFlexGridSizer::wxsFlexGridSizer( wxsItemResData* Data ):
  wxsSizer( Data, &Reg.Info ),
  Cols( 3 ),
  Rows( 0 ) {
}

wxSizer* wxsFlexGridSizer::OnBuildSizerPreview( wxWindow* Parent ) {
  wxFlexGridSizer* Sizer = new wxFlexGridSizer( Rows, Cols,
      VGap.GetPixels( Parent ), HGap.GetPixels( Parent ) );
  wxArrayInt _Cols = GetArray( GrowableCols );
  for( size_t i = 0; i < _Cols.Count(); i++ ) {
    Sizer->AddGrowableCol( _Cols[i] );
  }
  wxArrayInt _Rows = GetArray( GrowableRows );
  for( size_t i = 0; i < _Rows.Count(); i++ ) {
    Sizer->AddGrowableRow( _Rows[i] );
  }
  return Sizer;
}

void wxsFlexGridSizer::OnBuildSizerCreatingCode() {
  switch( GetLanguage() ) {
    case wxsCPP: {
      AddHeader( _T( "<wx/sizer.h>" ), GetInfo().ClassName, hfInPCH );
      Codef( _T( "%C(%d, %d, %s, %s);\n" ), Rows, Cols,
             VGap.GetPixelsCode( GetCoderContext() ).wx_str(),
             HGap.GetPixelsCode( GetCoderContext() ).wx_str() );
      wxArrayInt _Cols = GetArray( GrowableCols );
      for( size_t i = 0; i < _Cols.Count(); i++ ) {
        Codef( _T( "%AAddGrowableCol(%d);\n" ), _Cols[i] );
      }
      wxArrayInt _Rows = GetArray( GrowableRows );
      for( size_t i = 0; i < _Rows.Count(); i++ ) {
        Codef( _T( "%AAddGrowableRow(%d);\n" ), _Rows[i] );
      }
      return;
    }
    case wxsUnknownLanguage:
    default: {
      wxsCodeMarks::Unknown( _T( "wxsFlexGridSizer::OnBuildSizerCreatingCode" ), GetLanguage() );
    }
  }
}

void wxsFlexGridSizer::OnEnumSizerProperties( M_Code_unused long Flags ) {
  FixupList( GrowableCols );
  FixupList( GrowableRows );
  WXS_LONG( wxsFlexGridSizer, Cols, _T( "Cols" ), _T( "cols" ), 0 );
  WXS_LONG( wxsFlexGridSizer, Rows, _T( "Rows" ), _T( "rows" ), 0 );
  WXS_DIMENSION( wxsFlexGridSizer, VGap, _T( "V-Gap" ), _T( "V-Gap in dialog units" ), _T( "vgap" ), 0, false );
  WXS_DIMENSION( wxsFlexGridSizer, HGap, _T( "H-Gap" ), _T( "H,y-Gap in dialog units" ), _T( "hgap" ), 0, false );
  WXS_SHORT_STRING( wxsFlexGridSizer, GrowableCols, _T( "Growable cols" ), _T( "growablecols" ), _T( "" ), false );
  WXS_SHORT_STRING( wxsFlexGridSizer, GrowableRows, _T( "Growable rows" ), _T( "growablerows" ), _T( "" ), false );
  FixupList( GrowableCols );
  FixupList( GrowableRows );
}
